package top.kaoshanji.p2ch10a;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

/**
 * 代码清单-p2ch10a-316页a：错误使用finalize方法来关闭文件的示例
 * author kaoshanji
 * ^_^
 * create time 2025/2/2 18:18
 */
public class FileHolder {

    private Path path;
    private InputStream inputStream;

    public FileHolder(Path path) {
        this.path = path;
    }

    public void open() throws IOException {
        this.inputStream = Files.newInputStream(path, StandardOpenOption.WRITE);
    }

    @Override
    protected void finalize() throws Throwable {
        if (null != inputStream) {
            inputStream.close();
        }
        super.finalize();
    }
}
